package pl.edu.pw.stud.ltrzaska.filesystem;

import java.io.File;

import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;
import org.apache.log4j.Logger;

import pl.edu.pw.stud.ltrzaska.exception.FilesysPolicyException;

public class VirtualFsReader {
	private static final Logger LOGGER = Logger.getLogger(VirtualFsReader.class.getName());
	private String entryDir;

	public VirtualFsReader(File path) {
		setEntryDir(path.toString());
	}
	
	public VirtualFsReader(String path) {
		setEntryDir(path);
	}

	public void setEntryDir(String policyDir) {
		this.entryDir = policyDir;
	}

	public String getEntryDir() {
		return entryDir;
	}

	public FileObject resolve() throws FilesysPolicyException {
		FileSystemManager fsManager = null;
		FileObject fo = null;
		try {
			fsManager = VFS.getManager();

			fo = fsManager.resolveFile(getEntryDir());
			if (fo == null) {
				throw new FilesysPolicyException("Cannot resolve file. Null pointer occured.");
			}
			//TODO zostawic/usunac
			//FileType ft = fo.getType();

		} catch (FileSystemException e) {
			LOGGER.error("", e);
		}
		return fo;
	}
}
